#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef int BTDataType;

typedef struct BinaryTreeNode
{
	BTDataType data;
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;
}BTNode;
BTNode* BuyNode(BTDataType a)
{
	BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
	if (newnode == NULL)
	{
		perror("BinaryTreeCreate");
		return NULL;
	}
	newnode->data = a;
	newnode->left = NULL;
	newnode->right = NULL;
	return newnode;
}
BTNode* treecreate()
{
	BTNode* node1 = BuyNode(1);
	BTNode* node2 = BuyNode(2);
	BTNode* node3 = BuyNode(2);
	BTNode* node4 = BuyNode(3);
	BTNode* node5= BuyNode(3);
	BTNode* node6 = BuyNode(4);
	BTNode* node7 = BuyNode(4);
	node1->left = node2;
	node1->right = node3;
	node2->left = node4;
	node3->right = node5;
	node4->left = node6;
	node5->right = node7;
	return node1;
}
int TreeDepth(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	int  left = TreeDepth(root->left);
	int right = TreeDepth(root->right);
	return left > right ? left + 1 : right + 1;
}
bool isBalanced(BTNode* root) {
	if (root == NULL)
	{
		return true;
	}
	int leftdepth = TreeDepth(root->left);
	int rightdepth = TreeDepth(root->right);
	int result = leftdepth > rightdepth ? leftdepth - rightdepth : rightdepth - leftdepth;
	if (result > 1)
	{
		return false;
	}
	bool  leftresult = isBalanced(root->left);
	if (leftresult == false)
	{
		return false;
	}
	return isBalanced(root->right);

}
int main()
{
	BTNode *tree =  treecreate();
	bool result = isBalanced(tree);
	printf("%d", result);
	return 0;
}